package utils

import (
	"time"
	log "github.com/sirupsen/logrus"
	"github.com/go-ini/ini"
)

var (
	RunMode string

	PageSize  int
	JwtSecret string
	JwtSalt   string

	HTTPPort     string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration

	Type     string
	Username     string
	Password string
	Host     string
	Port string
	DBName string
)

func init() {
	file, err := ini.Load("conf/config.ini")
	if err != nil {
		log.Error("配置文件读取失败，请检查配置文件！")
	}
	LoadApp(file)
	LoadServer(file)
	LoadData(file)
}

func LoadApp(file *ini.File) {
	RunMode = file.Section("").Key("RUN_MODE").MustString("dev")
	PageSize = file.Section("app").Key("PAGE_SIZE").MustInt(10)
	JwtSecret = file.Section("app").Key("JWT_SECRET").MustString("!@)*#)!@U#@*!@!)")
	JwtSalt = file.Section("app").Key("JWT_SALT").MustString("!123")
}

func LoadServer(file *ini.File) {
	HTTPPort = file.Section("server").Key("HTTP_PORT").MustString(":3000")
	ReadTimeout = time.Duration(file.Section("server").Key("READ_TIMEOUT").MustInt(60)) * time.Second
	WriteTimeout = time.Duration(file.Section("server").Key("WRITE_TIMEOUT").MustInt(60)) * time.Second
}

func LoadData(file *ini.File) {
	Type = file.Section("database").Key("TYPE").Value()
	DBName = file.Section("database").Key("NAME").Value()
	Username = file.Section("database").Key("USERNAME").Value()
	Password = file.Section("database").Key("PASSWORD").Value()
	Host = file.Section("database").Key("HOST").MustString("127.0.0.1")
	Port = file.Section("database").Key("PORT").MustString("5432")
}